package com.hcms.admin.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hcms.admin.service.ISysResourceService;
import com.hcms.common.service.IOssService;
import com.hcms.mybatis.entity.SysResource;
import com.hcms.mybatis.mapper.SysResourceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 对象存储记录 服务实现类
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-23
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${cms.admin.resource-host:'/'}")
    private String resourceHost;

    @Autowired
    private IOssService ossService;

    @Autowired
    private SysResourceMapper sysResourceMapper;

    @Override
    public SysResource getOneBySha1(String sha1) {
        LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysResource::getSha1, sha1);
        return sysResourceMapper.selectOne(queryWrapper);
    }

    @Override
    public SysResource uploadObject(MultipartFile file, Long adminId) throws IOException {
        logger.info("Run uploadObject(), file={}, adminId={}", file.getOriginalFilename(), adminId);
        String host = resourceHost.endsWith("/") ? resourceHost : resourceHost + "/";
        String path = ossService.uploadObject(file);
        logger.info("File storage path: {}", path);
        String sha1 = SecureUtil.sha1(file.getInputStream());
        logger.info("File sha1: {}", sha1);
        // 保存文件到数据库中
        SysResource storage = new SysResource();
        storage.setName(file.getOriginalFilename());
        storage.setType(file.getContentType());
        storage.setSize(file.getSize());
        storage.setUrl(host + path);
        storage.setSha1(sha1);
        storage.setCreator(adminId);
        storage.setCreateTime(new Date());
        int insert = sysResourceMapper.insert(storage);
        if (insert > 0) {
            logger.info("Saved data successfully");
            return storage;
        } else {
            logger.error("Failed to save data");
        }
        return null;
    }

    @Override
    public boolean deleteObject(String url, Long adminId) throws FileNotFoundException {
        logger.info("从对象存储中删除资源");
        ossService.deleteObject(url);
        LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(UmsObjectStorage::getUrl, url);
        int delete = sysResourceMapper.delete(queryWrapper);
        return delete > 0;
    }
}
